package com.sport.ui.home;


import static android.graphics.Color.BLACK;
import static android.graphics.Color.WHITE;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sport.R;
import com.sport.databinding.FragmentHomeBinding;
import com.sport.pojo.Account;
import com.sport.pojo.ProductBean;
import com.sport.util.MySQLitOpenHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private BottomFragment bottomFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private String[] titles1 = {"今日打卡之体态正了！开心！！！", "谁还没做过这组帕梅拉我真的会伤心的ok？",
            "健康饮食最重要！！！"};
    private int[] imgs1 = {R.drawable.ten, R.drawable.six,
            R.drawable.four};
    //进店必买菜单列表数据
    private String[] titles2 = {"'驼背矫正 | 叔贵", "跑步干货 | 新手友好", "新手跑步 | 子章老师"};
    private int[] imgs2 = {R.drawable.seven, R.drawable.nine,
            R.drawable.eight};
    private Map<String, List<ProductBean>> map;
    private Button bt_note, bt_collect, bt_change;
    private TextView tv_username, tv_HowManyDays, tv_userquote, tv_fans, tv_follow;
    private ImageView iv_pic;
    MySQLitOpenHelper db;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        db = new MySQLitOpenHelper(view.getContext());
        if (getArguments() != null) {
            String name = getArguments().getString("name");
            binding.tvUsername.setText(name);
            System.out.println("已获取" + name);
//            Account checkAccount = new Account(name);
            List<Account> accounts = db.selectAccountByName(new Account(name));
            if (accounts.isEmpty()) {
                System.out.println("------------------怎么可能！！骗人的吧！！！-----------------");
            } else {
                System.out.println("不应该啊都到这里了该成功了吧");
                for (Account a : accounts) {
                    System.out.println("---------------------如果你有就给我输出看看---------------");
                    System.out.println(a);
                    binding.tvFans.setText(a.getFans());
                    binding.tvUserquote.setText(a.getQuote());
                    binding.tvFollow.setText(Integer.toString(a.getFollow()));
                    binding.tvHowManyDays.setText(Integer.toString(a.getDay()));
                    binding.ivPic.setImageResource(getimages(a.getPic()));

                    binding.tvFans.setTextColor(getResources().getColor(R.color.colorPrimary));
                    binding.tvUserquote.setTextColor(getResources().getColor(R.color.colorPrimary));
                    binding.tvFollow.setTextColor(getResources().getColor(R.color.colorPrimary));
                    binding.tvUsername.setTextColor(getResources().getColor(R.color.colorPrimary));
                    binding.tvHowManyDays.setTextColor(getResources().getColor(R.color.colorPrimary));
                    break;
                }
            }
        }
//        -------
        // 获取选中哪个button
        bt_note = (Button) binding.btNote;
        bt_collect = (Button) binding.btCollect;
        setData();
        clickEvent();
//        -------

        return view;
    }

    public static int getimages(String name) {
        Class drawable = R.drawable.class;
        Field field = null;
        try {
            field = drawable.getField(name);
            int images = field.getInt(field.getName());
            return images;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    private void setData() {
        map = new HashMap<>();
        List<ProductBean> list1 = new ArrayList<>();
        List<ProductBean> list2 = new ArrayList<>();
        for (int i = 0; i < titles1.length; i++) {
            ProductBean bean = new ProductBean();
            bean.setName(titles1[i]);
            bean.setImg(imgs1[i]);
            list1.add(bean);
        }
        map.put("1", list1);//将推荐菜单列表的数据添加到map集合中
        for (int i = 0; i < titles2.length; i++) {
            ProductBean bean = new ProductBean();
            bean.setName(titles2[i]);
            bean.setImg(imgs2[i]);
            list2.add(bean);
        }
        map.put("2", list2); //将进店必买菜单列表的数据添加到map集合中

    }

    private void clickEvent() {
        bt_note.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用switchData()方法填充Rightfragment中的数据
                switchData(map.get("1"));
                bt_note.setTextColor(WHITE);
                bt_note.setBackground(getResources().getDrawable(R.drawable.bt_click_after));
//                ---
                bt_collect.setTextColor(BLACK);
                bt_collect.setBackground(getResources().getDrawable(R.drawable.bt_click_before));
            }
        });
        bt_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchData(map.get("2"));
                bt_collect.setTextColor(WHITE);
                bt_collect.setBackground(getResources().getDrawable(R.drawable.bt_click_after));
//                ---
                bt_note.setTextColor(BLACK);
                bt_note.setBackground(getResources().getDrawable(R.drawable.bt_click_before));
            }
        });

        //设置首次进入界面后，默认需要显示的数据
        switchData(map.get("1"));
    }

    /**
     * 填充Fragment下部的Fragment，并传递列表数据list
     */
    public void switchData(List<ProductBean> list) {
//        获取fragmentManager

//        getChildFragmentManager().beginTransaction().replace(R.id.bottom, bottomFragment).commit();

        BottomFragment fragment = new BottomFragment();
//
        FragmentManager fm = getChildFragmentManager();
//        开启一个事务
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
//        通过调用getInstance()方法实例化RightFragment
        bottomFragment = new BottomFragment().getInstance(list);
//        调用fragmentTransaction 的replace()方法，动态加载rightFragment
        if (!bottomFragment.isAdded()) {
            fragmentTransaction.replace(R.id.bottom, bottomFragment, getTag()); // R.id.right是fragment的id
        } else {
            fragmentTransaction.show(bottomFragment);
        }
//         提交事务
        fragmentTransaction.commit();
//        }
    }
}


